import cv2
import numpy as np
import queue
from window_gluing_detect import cfg_parser, window_gluing_detector
def paused_detctor(cfg_path):
    # 解析配置文件
    cfg_data = cfg_parser(cfg_path)
    # 获取视频路径
    video_path = cfg_data.path
    # 打开视频文件
    cap = cv2.VideoCapture(video_path)
    # 获取视频的帧率
    fps = cap.get(cv2.CAP_PROP_FPS)
    # 创建检测器实例
    detector = window_gluing_detector(cfg_data=cfg_data)
    buffer = queue.Queue(maxsize=10000)
    # 检查视频是否成功打开
    if not cap.isOpened():
        print("Error: Could not open video.")
        return
    paused = False
    count = 0
    cv2.namedWindow('Frame', cv2.WINDOW_NORMAL)
    while True:
        if not paused:
            ret, frame = cap.read()
            if not ret:
                print("Video has ended or failed to read frame.")
                break
            detector.detect(frame, count, buffer)
            count += 1

        # 显示帧
        if frame is not None:
            cv2.imshow('Frame', frame)

        # 捕获键盘输入
        key = cv2.waitKey(30) & 0xFF

        # 按下空格键暂停/继续
        if key == ord(' '):
            paused = not paused

        # 按下 q 键退出
        if key == ord('q'):
            break

    # 释放资源并关闭窗口
    cap.release()
    cv2.destroyAllWindows()


if __name__ == "__main__":
    cfg_path = './cfg.yaml'
    
    paused_detctor(cfg_path)
    